﻿using AutoMapper;
using MediatR;
using S.Eartag.API.Application.Command.SheepFileConditionCommand;
using S.Eartag.Domain.Model;
using S.Eartag.Infrastructure.Impl;
using S.Eartag.Infrastructure.Interface;

namespace S.Eartag.API.Application.CommandHandlers.SheepFileConditionCommandHandler
{
    public class SheepFileConditionCreateCommandHandler : IRequestHandler<SheepFileConditionCreateCommand, int>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public SheepFileConditionCreateCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(SheepFileConditionCreateCommand request, CancellationToken cancellationToken)
        {
            var res = sheepFileRepository.All().Result.Where(x => x.EarNumber == request.EarNumber).FirstOrDefault();
            request.CreateBy = "张三";
            request.CreateDate = DateTime.Now;
            request.UpdateBy = null;
            request.UpdateDate = null;
            if (res != null)
            {
                return -1;
            }
            return await sheepFileRepository.Create(mapper.Map<SheepFile>(request));
        }
    }
}
